package com.Cychat.post.controller;


import com.Cychat.commons.entity.ResultMessage;
import com.Cychat.commons.entity.pojo.Comment;
import com.Cychat.post.entity.dto.CommentDTO;
import com.Cychat.post.entity.vo.CommentVO;
import com.Cychat.post.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: 崔航
 * @Date: 2025/10/28:14:32
 * @Description:
 */
@RestController
@RequestMapping("/api/comment")
@CrossOrigin
public class CommentController {
    @Autowired
    private CommentService commentService;
    /**
     * 获取帖子评论
     * @param postId
     * @return
     */
    @GetMapping("/getCommentsByPostId/{postId}/{userId}")
    public ResultMessage getCommentsByPostId(@PathVariable("postId") Integer postId, @PathVariable("userId") Long userId) {
        List<Comment> comments = commentService.getCommentsByPostId(postId);
        return ResultMessage.success(comments);
    }
    /**
     * 获取评论
     * @param rootId
     * @return
     */
    @GetMapping("/getCommentsByRootId/{rootId}/{userId}")
    public ResultMessage getCommentsByRootId(@PathVariable("rootId") Integer rootId, @PathVariable("userId") Long userId) {
        List<CommentDTO> comments = commentService.getCommentsByRootId(rootId);
        return ResultMessage.success(comments);
    }
    /**
     * 添加评论
     * @param comment
     * @return
     */
    @PostMapping("/addComment/{userId}")
    public ResultMessage addComment(@RequestBody CommentVO comment, @PathVariable("userId") Long userId) {
        Integer commentId = commentService.addComment(comment);
        return ResultMessage.success(commentId);
    }
    /**
     * 实现点赞自增
     * @param commentId
     * @return
     */
    @PutMapping("/like/{commentId}/{userId}")
    public ResultMessage like(@PathVariable("commentId") Integer commentId,@PathVariable("userId") Long userId) {
        Comment comment = commentService.getCommentById(commentId);
        comment.setLikeCount(comment.getLikeCount() + 1);
        commentService.updateById(comment);
        return ResultMessage.success();
    }

}
